﻿using System;
using System.Windows.Forms;

namespace SlidingPuzzle {

    /// <summary>
    /// Input dialog for number of times to scramble board.
    /// <BR>
    /// Author: David I. Schwartz (dis@mail.rit.edu)
    /// <BR>
    /// </summary>
    public partial class TextInputDialog : Form {

        // how many times to scramble the board:
        private int scrambles = 0; // default to out of bounds
        public int Scrambles { get { return scrambles; } }

        /// <summary>
        /// Make the scramble dialog window.
        /// </summary>
        public TextInputDialog() { 
            InitializeComponent();
            textBox.Text = scrambles + "";
        }

        /// <summary>
        /// Test the input number. If out of bounds (must be non-neg and less than Int32.Max), make user re-input.
        /// </summary>
        /// <param name="sender">Submit button</param>
        /// <param name="e"></param>
        private void doneButton_Click(object sender, EventArgs e) {

            // default message:
            string msg = "Can't parse input! Please try again.";

            // test user input:
            try {
                String input = textBox.Text;
                scrambles = int.Parse(input);
                if (scrambles <= 0) {
                    msg = "Input too low! Please try again.";
                    throw new Exception();
                }
                else if (scrambles > SlidingPuzzle.MAXSCRAMBLES) {
                    msg = "Input too high! Please try again.";
                    throw new Exception();
                }

            } catch (Exception) {
                scrambles = 0; // just in case user entered out-of-bounds value
                textBox.Text = "0";
                MessageBox.Show(msg);
            }

            // ensure everything is OK before quitting:
            if (scrambles > 0 && scrambles <= SlidingPuzzle.MAXSCRAMBLES) {
                this.Dispose();
            }

        } // method doneButton_Click


        private void CancelInputButton_Click(object sender, EventArgs e) {
            scrambles = 0;
            Dispose();
        }

    } // class TextInputDialog

} // namespace SlidingPuzzle
